package com.foreknow.eldclient.service.impl;

import com.foreknow.eldclient.dao.FoodMapper;
import com.foreknow.eldclient.dao.OrderDetailetMapper;
import com.foreknow.eldclient.po.Food;
import com.foreknow.eldclient.po.OrderDetailet;
import com.foreknow.eldclient.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper FM;
    @Autowired
    private OrderDetailetMapper ODM;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception {
        return FM.listFoodByBusinessId(businessId);
    }

    @Override
    public List<Food> listFoodByFoodName(String foodName) throws Exception {
        return FM.listFoodByFoodName(foodName);
    }

    @Override
    public int addFoodByBId(Food food) {
        return FM.addFoodByBId(food);
    }

    @Override
    public int delFood(String foodId) {
        return FM.delFood(foodId);
    }

    @Override
    public int updateFoodById(Food food) {
        return FM.updateFoodById(food);
    }

    @Override
    public Food listFoodById(int foodId) {
        return FM.listFoodById(foodId);
    }

    @Override
    public int updateFoodSales(OrderDetailet orderDetailet) throws Exception {
        List<OrderDetailet> orderDetailets = ODM.getODByOrderId(orderDetailet.getOrderId());
        for (int i = 0; i < orderDetailets.size(); i++) {
            Food food = new Food();
            Integer foodId = orderDetailets.get(i).getFoodId();
            Integer quantity = orderDetailets.get(i).getQuantity();
            int foodSales = FM.getFoodSalesByFId(foodId);
            foodSales += quantity;
            food.setFoodId(foodId);
            food.setFoodSales(foodSales);

            FM.updateFoodSales(food);
        }
            return 1;
    }

    @Override
    public int getFoodSalesByFId(int foodId) {
        return FM.getFoodSalesByFId(foodId);
    }

    @Override
    public List<Food> listTop5Food() {
        return FM.listTop5Food();
    }


}
